package com.yiding.sys.common.utils;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.IdUtil;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import java.util.HashMap;
import java.util.Map;

/**
 * @author War horse imwarhorse@aliyun.com
 * Date: 2020/8/7
 * Description: 加密工具
 */

public class HashUtil {

	private final static String HASH_ALG = "MD5";
	private final static int HASH_ITERATIONS = 128;

	/**
	 * 生成密码（密文）
	 *
	 * @param username 用户名
	 * @param password 密码
	 * @return 加密后密码密文字符串
	 */
	public static Map<String, String> generatePassword(String username, String password) {
		Map<String, String> map = new HashMap<>();

		String uuid = IdUtil.simpleUUID();
		map.put("salt", uuid);  // 实际上并非salt，只是对应db table中的salt field
		Object realSalt = ByteSource.Util.bytes(uuid);
		SimpleHash hash = new SimpleHash(HASH_ALG, password, realSalt, HASH_ITERATIONS);
		map.put("hash", hash.toString());
		return map;
	}

}
